﻿using System;
using System.Web.Mvc;
using Business;
using Business.DbConnect;
using PagedList;

namespace ServerMotorManagement.Controllers
{
    [Authorize]
    public class CustomerController : Controller
    {
        //
        // GET: /Customer/

        public ActionResult Index(int? page)
        {
            int pageNumber = (page ?? 1);
            var customer = CustomerBusiness.SearchCustomerByText(String.Empty);
            return View(customer.ToPagedList(pageNumber, 3));
        }

        public ActionResult listCustomer(int? page)
        {
            var customer = CustomerBusiness.SearchCustomerByText(String.Empty);
            int pageNumber = (page ?? 1);
            return PartialView("listCustomer", customer.ToPagedList(pageNumber, 3));
        }

        public ActionResult Edit(int id)
        {
            return PartialView(CustomerBusiness.Select(id));
        }

        [HttpPost]
        public ActionResult Edit(Customer cs, FormCollection collection)
        {
            //Business.DbConnect.Customer cs = new Customer();
            IFormatProvider iFP = new System.Globalization.CultureInfo("vi-VN", true);
            cs.CreateDate = DateTime.Parse(collection["CreateDate"], iFP);
            var result = CustomerBusiness.Update(cs);
            return PartialView(cs);
        }

        public ActionResult Create()
        {
            return PartialView();
        }

        [HttpPost]
        public ActionResult Create(Customer cs, FormCollection collection)
        {
            IFormatProvider iFP = new System.Globalization.CultureInfo("vi-VN", true);
            cs.CreateDate = DateTime.Parse(collection["CreateDate"], iFP);
            var result = CustomerBusiness.Insert(cs);
            return PartialView(cs);
        }

        [HttpPost]
        public ActionResult Search(String sContent, string currentFilter, int? page)
        {
            if (Request.HttpMethod == "GET")
            {
                sContent = currentFilter;
            }
            else
            {
                page = 1;
            }
            ViewBag.CurrentFilter = sContent;
            int pageSize = 3;
            int pageNumber = (page ?? 1);
            var customer = CustomerBusiness.SearchCustomerByText(sContent);
            return PartialView("listCustomer", customer.ToPagedList(pageNumber, pageSize));
        }
    }
}